package com.zhinong.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhinong.dto.OrderPageQueryDTO;
import com.zhinong.dto.ProductPageQueryDTO;
import com.zhinong.entity.Order;
import com.zhinong.entity.Product;
import com.zhinong.mapper.OrderMapper;
import com.zhinong.result.PageResult;
import com.zhinong.service.OrderService;
import com.zhinong.utils.RedisIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {


    @Autowired

    private OrderMapper orderMapper;

    @Autowired
    private RedisIdWorker redisIdWorker;

    @Override
    public PageResult pageQuery(OrderPageQueryDTO orderPageQueryDTO) {
        PageHelper.startPage(orderPageQueryDTO.getPage(),orderPageQueryDTO.getPageSize());
        Page<Order> page = orderMapper.pageQuery(orderPageQueryDTO);
        long total = page.getTotal();
        List<Order> records = page.getResult();
        return new PageResult(total,records);
    }

    @Override
    public void deleteById(long id) {
        orderMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(Order order) throws Exception {
        if(true){
            throw new Exception("出错了");
        }
        Long id = redisIdWorker.nextId("order");
        order.setId(String.valueOf(id));
        orderMapper.insert(order);
    }

    @Override
    public void update(Order order) {
        orderMapper.update(order);
    }
}
